
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.ArrayList;

public class BigCalTest {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            System.out.print("Calculator> ");
            String str = br.readLine();
            if (str.equalsIgnoreCase("exit")) {
                break;
            }
            BigInteger result = calculate(str);
            System.out.println(result);
        }
        br.close();
    }

    public static BigInteger calculate(String str) {
        where = 0;
        return f(str.toCharArray(), 0);
    }

    public static int where;

    // s[i....]开始计算，遇到字符串终止 或者 遇到)停止
    // 返回 : 自己负责的这一段，计算的结果
    // 返回之前，更新全局变量where，为了调用自己的上一层函数知道从哪继续
    public static BigInteger f(char[] s, int i) {
        BigInteger cur = BigInteger.ZERO;
        ArrayList<BigInteger> numbers = new ArrayList<>();
        ArrayList<Character> ops = new ArrayList<>();
        while (i < s.length && s[i] != ')') {
            if (s[i] >= '0' && s[i] <= '9') {
                cur = cur.multiply(BigInteger.TEN).add(BigInteger.valueOf(s[i++] - '0'));
            } else if (s[i] != '(') {
                // 遇到了运算符 + - * /
                push(numbers, ops, cur, s[i++]);
                cur = BigInteger.ZERO;
            } else {
                // 遇到了左括号
                cur = f(s, i + 1);
                i = where + 1;
            }
        }
        push(numbers, ops, cur, '+');
        where = i;
        return compute(numbers, ops);
    }

    public static void push(ArrayList<BigInteger> numbers, ArrayList<Character> ops, BigInteger cur, char op) {
        int n = numbers.size();
        if (n == 0 || ops.get(n - 1) == '+' || ops.get(n - 1) == '-') {
            numbers.add(cur);
            ops.add(op);
        } else {
            BigInteger topNumber = numbers.get(n - 1);
            char topOp = ops.get(n - 1);
            if (topOp == '*') {
                numbers.set(n - 1, topNumber.multiply(cur));
            } else {
                numbers.set(n - 1, topNumber.divide(cur));
            }
            ops.set(n - 1, op);
        }
    }

    public static BigInteger compute(ArrayList<BigInteger> numbers, ArrayList<Character> ops) {
        int n = numbers.size();
        BigInteger ans = numbers.get(0);
        for (int i = 1; i < n; i++) {
            if (ops.get(i - 1) == '+') {
                ans = ans.add(numbers.get(i));
            } else {
                ans = ans.subtract(numbers.get(i));
            }
        }
        return ans;
    }
}
